package com.heima.kafka.listener;


import com.alibaba.fastjson.JSON;
import com.heima.kafka.controller.com.heima.kafka.pojo.User;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.KafkaListeners;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * 监听类
 */
@Component
public class HelloListener {
    @KafkaListener(topics = "hello-itcast")
    public void receiverMessage(ConsumerRecord<?, ?> record) {
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if (optional.isPresent()) {
            Object value = record.value();
            User user = JSON.parseObject((String) value, User.class);
            System.out.println(user);
        }
    }

    // 或者使用
    @KafkaListener(topics = "kafka-hello")
    public void receiverMessage(String message) {
        if (!StringUtils.isEmpty(message)) {
            System.out.println(message);
        }
    }
}
